package main

import (
	"fmt"
	"time"
)
// 字符串转日期
func StrToTime(str string) (time.Time) {
	//str := "2021-01-03 15:23:11"
	// 设置时区
	loc, _ := time.LoadLocation("Asia/Shanghai")
	d, _ := time.ParseInLocation("2006-01-02 15:04:05", str, loc)
	fmt.Printf("time: %v\n", d)
	return d
}
// timie z转字符串
func timeToStr() string {
	now := time.Now()
	fmt.Printf("%v~~~ 类型是%T \n", now, now)
	fmt.Printf("年 %v \n", now.Year())
	fmt.Printf("%d-%d-%d %d-%d-%d \n", now.Year(),now.Month(),now.Day(), now.Hour(), now.Minute(),now.Second())
	sprintf := fmt.Sprintf("%d-%d-%d %d:%d:%d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
	fmt.Println(sprintf)

	format := now.Format("2006-01-02 15:04:08") // 得到当前格式的字符串
	fmt.Println(format)
	return format
}

// 获取几天前或者几天后
func GetDateBefore(t *time.Time, day int) time.Time {
	//now := t //time.Now()
	//day := 3
	//获取3天前
	before := t.AddDate(0, 0, day) // 如果是3天后，则将 - 去掉
	fmt.Printf("time: %v\n", before)
	return before
}

func main() {
	t := time.Now()
	before := GetDateBefore(&t, 1)
	fmt.Println(before)
}

